package cn.shopping.controller.advertising;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.shopping.pojo.Advertising;
import cn.shopping.pojo.Sh_two;
import cn.shopping.service.advertising.AdvertisingService;

@Controller
@RequestMapping("/Advertis")
public class AdvertisingController {

	@Resource
	private AdvertisingService advertisingService;
	
	/**
	 * 显示类别数据
	 * @return
	 */
	@RequestMapping("/seloftwo")
	@ResponseBody
	public List<Sh_two> seloftwo() {
		List<Sh_two> list = advertisingService.getallofland();
		return list;
	}
	
	/**
	 * 新增
	 * @param session
	 * @param request
	 * @param land
	 * @param totals
	 * @param content
	 * @param picture
	 * @return
	 */
	@RequestMapping("/addadvertis.html")
	public String addadvertis(HttpSession session,HttpServletRequest request,
			@RequestParam(value="land") int land,
			@RequestParam(value="totals") String totals,
			@RequestParam(value="content") String content,
			@RequestParam(value="picture") MultipartFile picture) {
		Advertising ads = new Advertising();
		if(!picture.isEmpty()){
			String path = request.getSession().getServletContext()
					.getRealPath("shoppingadmin"+File.separator+"images"+File.separator+"advertisimg");
			String picname = System.currentTimeMillis()+".jpg";
			System.out.println(picname+""+path);
			ads.setAdvertisingphonto(picname);
			File file = new File(path,picname);
			try {
				picture.transferTo(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ads.setAdvertisingcontent(content);
		ads.setAdvertisingTotal(totals);
		ads.setLandId(land);
		if (advertisingService.addAdvertising(ads)) {
			return "redirect:/Advertis/seladvertising.html";
		}
		return "message_edit";
	}
	
	/**
	 * 显示数据
	 * @param model
	 * @return
	 */
	@RequestMapping("/seladvertising.html")
	public String  seladvertising(Model model) {
		List<Advertising> list = advertisingService.getAdvertising();
		model.addAttribute("Advertisinglist", list);
		return "sjbanner";
	}
	
	/**
	 * 删除
	 * @param aid
	 * @return
	 */
	@RequestMapping("/deladvertisings.html/{aid}")
	public String  deladvertisings(@PathVariable int aid,HttpSession session,HttpServletRequest request) {
		Advertising ads = advertisingService.getAdvertisingbyid(aid);
		String pname = ads.getAdvertisingphonto();
		String path = request.getSession().getServletContext()
				.getRealPath("shoppingadmin"+File.separator+"images"+File.separator+"advertisimg");
		File file = new File(path,pname);
		try {
			file.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (advertisingService.delAdvertising(aid)) {
			return "redirect:/Advertis/seladvertising.html";
		}
		return "redirect:/Advertis/seladvertising.html";
	}
	
	/**
	 * 修改
	 * @param avid
	 * @param model
	 * @return
	 */
	//获取信息
	@RequestMapping("/seladvertisingByid.html")
	public String  seladvertisingByid(@RequestParam("avid") int avid,Model model,HttpSession session) {
		Advertising ads = advertisingService.getAdvertisingbyid(avid);
		model.addAttribute("advetis", ads);
		session.setAttribute("xx", ads.getAdvertisingphonto());
		return "modifymessage_edit";
	}
	
	//保存修改
	@RequestMapping("/updadvertis.html")
	public String updadvertis(HttpSession session,HttpServletRequest request,
			@RequestParam(value="aid") int aid,
			@RequestParam(value="landofid") int land,
			@RequestParam(value="totals") String totals,
			@RequestParam(value="content") String content,
			@RequestParam(value="picture") MultipartFile picture) {
		Advertising ads = advertisingService.getAdvertisingbyid(aid);
		String oldpicnaems = session.getAttribute("xx").toString();
		if(!picture.isEmpty()){
			String path = request.getSession().getServletContext()
					.getRealPath("shoppingadmin"+File.separator+"images"+File.separator+"advertisimg");
			String picname = System.currentTimeMillis()+".jpg";
			System.out.println(picname+""+path);
			ads.setAdvertisingphonto(picname);
			File file = new File(path,picname);
			try {
				picture.transferTo(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
			File file2 = new File(path,oldpicnaems);
			try {
				file2.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			ads.setAdvertisingphonto(oldpicnaems);
		}
		ads.setAdvertisingcontent(content);
		ads.setAdvertisingTotal(totals);
		ads.setLandId(land);
		if (advertisingService.updAdvertising(ads)) {
			return "redirect:/Advertis/seladvertising.html";
		}
		return "modifymessage_edit";
		
	}
}
